package com.youkeyi.ddy.cloud.application.business.hr.employee.certificate.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.BusinessEmployeeCertificateCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.dto.BusinessEmployeeCertificateListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.dto.BusinessEmployeeCertificateListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.param.BusinessEmployeeCertificateCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.param.BusinessEmployeeCertificateDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.param.BusinessEmployeeCertificateListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.certificate.param.BusinessEmployeeCertificateUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.HrEmployeeCertificateCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.dto.HrEmployeeCertificateListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.param.HrEmployeeCertificateCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.param.HrEmployeeCertificateDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.param.HrEmployeeCertificateQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.certificate.param.HrEmployeeCertificateUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * BusinessEmployeeCertificateController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 11:26 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeCertificateController implements BusinessEmployeeCertificateCloudService {

    @Autowired
    private HrEmployeeCertificateCloudService hrEmployeeCertificateCloudService;

    @Override
    public BusinessEmployeeCertificateListCloudDto list(BusinessEmployeeCertificateListCloudParam param) {
        BusinessEmployeeCertificateListCloudDto dto = new BusinessEmployeeCertificateListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeCertificateQueryCloudParam employeeQueryCloudParam = new HrEmployeeCertificateQueryCloudParam();
        employeeQueryCloudParam.setEmployeeCertificatePrimaryKeys(param.getEmployeeCertificatePrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeCertificateListCloudDto employeeDtos = hrEmployeeCertificateCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<BusinessEmployeeCertificateListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeCertificateListItemCloudDto item = new BusinessEmployeeCertificateListItemCloudDto();
                item.setEmployeeCertificatePrimaryKey(employee.getEmployeeCertificatePrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setIssueDate(employee.getIssueDate());
                item.setExpiryDate(employee.getExpiryDate());
                item.setCertificateName(employee.getCertificateName());
                item.setIssuingAuthority(employee.getIssuingAuthority());
                item.setAttachmentKey(employee.getAttachmentKey());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeCertificateCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCertificateCreateCloudParam employeeCreateCloudParam = new HrEmployeeCertificateCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setIssueDate(param.getIssueDate());
        employeeCreateCloudParam.setExpiryDate(param.getExpiryDate());
        employeeCreateCloudParam.setCertificateName(param.getCertificateName());
        employeeCreateCloudParam.setIssuingAuthority(param.getIssuingAuthority());
        employeeCreateCloudParam.setAttachmentKey(param.getAttachmentKey());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCertificateCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeCertificateUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeCertificatePrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCertificateUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeCertificateUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeCertificatePrimaryKey(param.getEmployeeCertificatePrimaryKey());
        employeeUpdateCloudParam.setIssueDate(param.getIssueDate());
        employeeUpdateCloudParam.setExpiryDate(param.getExpiryDate());
        employeeUpdateCloudParam.setCertificateName(param.getCertificateName());
        employeeUpdateCloudParam.setIssuingAuthority(param.getIssuingAuthority());
        employeeUpdateCloudParam.setAttachmentKey(param.getAttachmentKey());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCertificateCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeCertificateDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeCertificatePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCertificateDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeCertificateDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeCertificatePrimaryKeys(param.getEmployeeCertificatePrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCertificateCloudService.delete(employeeDeleteCloudParam);
    }
}
